package com.yxw.yxnet_cd_center.customer_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.constant.RedisIdConstant;
import com.yxw.yxnet_cd_center.customer_service.dto.DictQueryDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.RuleQueryDTO;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsChannelEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsPortRuleEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.ChannelMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.DictMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.PortMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxCsPortRuleMapper;
import com.yxw.yxnet_cd_center.customer_service.service.DictService;
import com.yxw.yxnet_cd_center.customer_service.utils.RedisManager;
import com.yxw.yxnet_cd_center.customer_service.vo.ChannelVo;
import com.yxw.yxnet_cd_center.customer_service.vo.DictionaryVo;
import com.yxw.yxnet_cd_center.customer_service.vo.PortVo;
import com.yxw.yxnet_cd_center.customer_service.vo.YxCsPortRuleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典表管理
 * @author liuxin
 * @date 2023.09.17
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private PortMapper portMapper;

    @Autowired
    private YxCsPortRuleMapper yxCsPortRuleMapper;

    /**
     * 根据类型获取字典值列表
     * @param dto
     * @return
     */
    @Override
    public List<DictionaryVo> getDictDataByType(DictQueryDTO dto) {
        //分流规则要求查询启用和禁用状态的，这里不从缓存拿
        List<DictionaryVo> dataList=null;
        if(null!=dto.getStatus()){
            dataList=dictMapper.getDictDataByType(dto);
        }else{
            dataList=(List<DictionaryVo>)redisManager.get(RedisIdConstant.DICT_DATA+"_"+dto.getDictType());
            if(null==dataList || dataList.isEmpty()){
                dataList=dictMapper.getDictDataByType(dto);
                if(null!=dataList && !dataList.isEmpty()){
                    //存放12小时
                    redisManager.set(RedisIdConstant.DICT_DATA+"_"+dto.getDictType(),dataList,12 * 60 * 60);
                }
            }
        }
        return dataList;
    }

    /**
     * 根据ID查询字典值
     * @param id
     * @return
     */
    @Override
    public DictionaryVo getDictDataById(Long id) {
        return dictMapper.getDictDataById(id);
    }

    /**
     * 查询渠道列表
     * @return
     */
    @Override
    public List<ChannelVo> getChannelList() {
        LambdaQueryWrapper<YxCsChannelEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<YxCsChannelEntity> entityList = channelMapper.selectList(queryWrapper);
        List<ChannelVo> voList = new ArrayList<>();
        if(null!=entityList && !entityList.isEmpty()){
            voList = BeanUtil.copyToList(entityList, ChannelVo.class);
        }
        return voList;
    }

    /**
     * 根据渠道查询端口列表
     * @param channelId
     * @return
     */
    @Override
    public List<PortVo> getPortList(Long channelId) {
        return portMapper.getPortList(channelId);
    }

    /**
     * 字段表状态修改
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDictStatus(Long id) {
        dictMapper.updateDictStatus(id);
        DictionaryVo vo = dictMapper.getDictDataById(id);
        if(vo!=null){
            //删除redis缓存
            redisManager.del(RedisIdConstant.DICT_DATA+"_"+vo.getType());
        }
    }

    /**
     * 根据查询规则列表明细列表
     * @param dto
     * @return
     */
    @Override
    public List<YxCsPortRuleVO> getRuleList(RuleQueryDTO dto) {
        List<YxCsPortRuleVO> voList=new ArrayList<>() ;
        LambdaQueryWrapper<YxCsPortRuleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true,YxCsPortRuleEntity::getPortId,dto.getPortId())
                .eq(true,YxCsPortRuleEntity::getServiceType,dto.getServiceType())
                .eq(true,YxCsPortRuleEntity::getRuleType,dto.getRuleType())
                .eq(true,YxCsPortRuleEntity::getStatus, CommonConstant.ZERO);
        List<YxCsPortRuleEntity> entityList = yxCsPortRuleMapper.selectList(queryWrapper);
        if(null!=entityList && !entityList.isEmpty()){
            voList=BeanUtil.copyToList(entityList,YxCsPortRuleVO.class);
        }
        return voList;
    }
}
